<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Numerals</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="flashcard.css" rel="stylesheet" type="text/css">
</head>

<?php

require_once 'Numbers/Words.php';

$nwt =& new Numbers_Words();
//echo $foo->toWords(80973214);

function roman2arabic($roman) {
	$romans = array(
	    'M' => 1000,
	    'CM' => 900,
	    'D' => 500,
	    'CD' => 400,
	    'C' => 100,
	    'XC' => 90,
	    'L' => 50,
	    'XL' => 40,
	    'X' => 10,
	    'IX' => 9,
	    'V' => 5,
	    'IV' => 4,
	    'I' => 1,
	);

	$result = 0;

	foreach ($romans as $key => $value) {
	    while (strpos($roman, $key) === 0) {
	        $result += $value;
	        $roman = substr($roman, strlen($key));
	    }
	}
	return $result;
}

function ordinal($number) {
	$ends = array('th','st','nd','rd','th','th','th','th','th','th');
	if (($number % 100) >= 11 && ($number % 100) <= 13)
	   $ordinal = $number.'th';
	else
	   $ordinal = $number.$ends[$number % 10];
	
	return $ordinal;
}

try {
	$nouns = array();
	$db = new PDO('sqlite:Latin.db3');
	$q = $db->query('SELECT * FROM numerals;');
	$tmp = $q->fetchAll(PDO::FETCH_ASSOC);
	$tmp2 = array_rand($tmp, 5);
	$words = array();
	foreach($tmp2 as $n) {
		$number = roman2arabic($tmp[$n]['roman']);
		$enum = $nwt->toWords($number);
		$adverb = "";
		if ($number == 1) {
			$adverb = "once";
		} elseif ($number == 2) {
			$adverb = "twice";
		} elseif ($number == 3) {
			$adverb = "thrice";
		} else {
			$adverb = "$enum times";
		}
		
		$words[] = array($enum, $tmp[$n]['cardinal']." <span class=\"smallcaps\">".$tmp[$n]['roman']."</span>");
		$words[] = array(ordinal($number), $tmp[$n]['ordinal']);
		$words[] = array("by $enum", $tmp[$n]['distributive']);
		$words[] = array($adverb, $tmp[$n]['adverb']);
	}

	shuffle($words);
	
	$db = NULL;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>
<script type="text/javascript">
<?php
print "var words = new Array(".count($words).");\n";
for($i = 0; $i < count($words); $i++) {
	$tmp = implode("', '", $words[$i]);
	$tmp = preg_replace("/\s*<i>(.*?)<\/i>(.*)$/", " $2 <span class=\"wl\">$1</span>", $tmp);
	print "words[$i] = ['$tmp'];\n";
	
}
?>

var shown = 0;

function updateVoc() {
	var i = Math.floor(shown / 2);

	var j = shown % 2;

	if (j == 0) {
		document.getElementById('v3').innerHTML = words[i][0];
		document.getElementById('v1').innerHTML = "";
	} else {
		document.getElementById('v3').innerHTML = words[i][0];
		document.getElementById('v1').innerHTML = words[i][1];
	}
}

function next() {
	shown++;
	if (shown > words.length * 2 - 1) {
		shown = 0;
		alert('All done!');
	}
	updateVoc();
}

function prev() {
	shown--;
	if (shown < 0) {
		shown = words.length * 2 - 1;
		alert('All done!');
	}
	updateVoc();
}

</script>

<body onload="updateVoc()">

<h1 style="text-align:center; color: #60B0C0;">Latin Numerals</h1>

<div class="center">
<div id="blank" class="v2">&nbsp;</div>
<div id="v3" class="v3"></div>
<div id="blank" class="v2">&nbsp;</div>
<div id="v1" class="v1"></div>


<a href="#" class="button" id="btnprev" onclick="prev()" accesskey="p">&lt;&lt;</a>
<a href="#" class="button" id="button" onclick="location.reload();">new</a>
<a href="#" class="button" id="btnnext" onclick="next()" accesskey="n">&gt;&gt;</a>


</div>

</body>
</html>
